package org.monster.app;

import org.monster.boot.Constants;

/**
 * dos替换
 *
 * @author Monster
 */
public class DosReplace implements Replace {

    /**
     * 替换
     * 字符串的- --为空格，换行
     *
     * @param dosCommand dos命令
     * @return dos命令
     */
    @Override
    public String replace(String dosCommand) {
        dosCommand = getDosCommand(dosCommand);
        dosCommand = execute(dosCommand);
        return dosCommand;
    }

    /**
     * 获取dos命令
     * 所需替换的字符
     *
     * @param dosCommand dos命令
     * @return dos命令
     */
    private String getDosCommand(String dosCommand) {
        int startSym = dosCommand.indexOf('{');
        int endSym = dosCommand.lastIndexOf('}');
        if (startSym != -1 && endSym != -1) {
            dosCommand = dosCommand.substring(startSym + 1, endSym);
        }
        return dosCommand;
    }

    /**
     * 替换
     *
     * @param dosCommand dos命令
     * @return dos命令
     */
    public String execute(String dosCommand) {
        if (dosCommand.contains(Constants.STR_B))
            dosCommand = dosCommand.replace(Constants.STR_B, "\r\n");
        else if (dosCommand.contains(Constants.STR_A))
            dosCommand = dosCommand.replace(Constants.STR_A, " ");
        return dosCommand;
    }
}
